package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import cn.wolfcode.vo.StartAuditVO;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.prefs.BackingStoreException;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;

    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;

    @Autowired
    private RuntimeService runtimeService;
    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        //防止非法上架的操作
        ServiceItem newItem = new ServiceItem();
        newItem.setName(serviceItem.getName());
        newItem.setOriginalPrice(serviceItem.getOriginalPrice());
        newItem.setDiscountPrice(serviceItem.getDiscountPrice());
        newItem.setCarPackage(serviceItem.getCarPackage());
        newItem.setServiceCatalog(serviceItem.getServiceCatalog());
        newItem.setInfo(serviceItem.getInfo());

        //1> 判断他是否为套餐,如果是套餐就把他的状态设置为初始化状态
        //这样如果新增的数据不合理就可以,在初始化的状态下可以去修改数据
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            newItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else {
            //如果不是套餐就设置为无需审核的状态
            newItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        //设置时间
        newItem.setCreateTime(new Date());
        serviceItemMapper.insert(newItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldItem = this.get(serviceItem.getId());
        //判断他是否是审核中,或者是上架状态,如果是抛出异常
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(oldItem.getAuditStatus())
            || ServiceItem.SALESTATUS_ON.equals(oldItem.getSaleStatus())) {
            throw new BusinessException("非法操作");
        }
        //先把查询出来的状态设置到serviceItem中
        serviceItem.setAuditStatus(oldItem.getAuditStatus());
        //如果数据是套餐并且审核状态为通过状态就把状态改为初始化状态
        if (oldItem.getCarPackage().equals(ServiceItem.CARPACKAGE_YES)
        && oldItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }

        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = this.get(id);
        //判断状态是否为下架状态
        if (!serviceItem.getSaleStatus().equals(ServiceItem.SALESTATUS_OFF)) {
            throw new BusinessException("非法操作");
        }

        //判断数据如果是套餐,但是审核状态为非审核通过状态,抛出异常
        if (serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_YES)
        && !serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)) {
            throw new BusinessException("非法操作");
        }

        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void saleOff(Long id) {
        //先查询数据
        ServiceItem serviceItem = this.get(id);
        //判断是否为上架状态
        if (!serviceItem.getSaleStatus().equals(ServiceItem.SALESTATUS_ON)) {
            throw new BusinessException("非法操作");
        }

        //判断数据是否是套餐,并且审核状态为通过状态
        if (serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_YES)
        && !serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)) {
            throw new BusinessException("非法操作");
        }
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);

    }

    @Override
    public void startAudit(StartAuditVO vo) {
        //业务逻辑校验,判断是否是套餐,判断如果是审核中或者是审核通过的状态
        ServiceItem serviceItem = get(vo.getId());
        if (serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_NO)) {
            throw new BusinessException("非法操作");
        }
        if (serviceItem.getSaleStatus().equals(ServiceItem.AUDITSTATUS_AUDITING)
        || serviceItem.getSaleStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)
        ) {
            throw new BusinessException("非法操作");
        }
        //把信息保存到CarPackageAudit当中
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setServiceItemId(serviceItem.getId());
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setInfo(vo.getInfo());
        carPackageAudit.setAuditorId(vo.getDirector());
        carPackageAudit.setBpmnInfoId(vo.getBpmnInfoId());
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        //把数据保存到数据库中
        carPackageAuditMapper.insert(carPackageAudit);
        //根据id去查询bpmnInfo信息
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(vo.getBpmnInfoId());
        //设置流程变量
        Map<String , Object> map = new HashMap<>();
        map.put("director",vo.getDirector());
        if (vo.getFinance() != null) {
            map.put("finance",vo.getFinance());
        }
        map.put("disCountPrice",serviceItem.getDiscountPrice().longValue());
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId().toString(), map);
        //设置流程实例id
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
        //修改养修服务单项的状态
        serviceItemMapper.changeAuditStatus(vo.getId() , ServiceItem.AUDITSTATUS_AUDITING);
    }


}
